package com.karaoke.android.library.mvvm

import android.app.DialogFragment
import android.content.Context
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.karaoke.android.library.mvvm.BindingConfig
import com.karaoke.android.library.mvvm.FragmentViewModel

/**
 * Created by allen on 16-12-9.
 */

abstract class MvvmDialogFragment<B : ViewDataBinding, out VM : FragmentViewModel> : DialogFragment()
{
    abstract val viewModel: VM
    abstract val mConfig: BindingConfig
    lateinit var mBinding: B

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        mBinding = DataBindingUtil.inflate(inflater, mConfig.layoutId, container, false)
        mBinding.setVariable(mConfig.bindingVariable, viewModel)
        return mBinding.root
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)
        viewModel.onViewCreated(view, savedInstanceState)
    }

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        viewModel.onCreate(savedInstanceState)
    }

    override fun onAttach(context: Context?)
    {
        super.onAttach(context)
        viewModel.onAttach(context)
    }

    override fun onResume()
    {
        super.onResume()
        viewModel.onResume()
    }

    override fun onStart()
    {
        super.onStart()
        viewModel.onStart()
    }

    override fun onPause()
    {
        super.onPause()
        viewModel.onPause()
    }

    override fun onDestroy()
    {
        super.onDestroy()
        viewModel.onDestroy()
    }

    override fun onDetach()
    {
        super.onDetach()
        viewModel.onDetach()
    }

    override fun onDestroyView()
    {
        super.onDestroyView()
        viewModel.onDestroyView()
    }
}
